package com.bls.service.impl;

import com.bls.action.BLSAction;
import com.bls.constant.ResultCodeEnum;
import com.bls.dao.DraftDao;
import com.bls.entity.Analyse;
import com.bls.entity.Draft;
import com.bls.entity.query.DraftQuery;
import com.bls.entity.vo.DraftVo;
import com.bls.exception.MyException;
import com.bls.remote.service.CoreRemoteService;
import com.bls.service.AnalyseService;
import com.bls.service.DraftService;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * (Draft)表服务实现类
 *
 * @author makejava
 * @since 2022-01-10 18:42:44
 */
public class DraftServiceImpl implements DraftService {


    @Override
    public List<DraftVo> pageConditionDraftList(DraftQuery draftQuery, Integer pageSize, Integer current) throws Exception {
        CoreRemoteService coreRemoteService = BLSAction.getBean(CoreRemoteService.class);
        DraftDao draftDao = BLSAction.getBean(DraftDao.class);
        final Connection connection = BLSAction.getDBConnection();
        List<DraftVo> draftVoList = null;
        try {
            draftVoList = draftDao.pageConditionDraftList(connection, draftQuery, pageSize, current);
//            for (DraftVo draftVo : draftVoList) {
//                // 查询封装帮区信息 todo
////                JSONObject bandInfo = coreRemoteService.getBandInfo(draftVo.getBandId());
////                if (bandInfo == null || ((int) bandInfo.get("total")) == 0) {
////                    throw new MyException(ResultCodeEnum.BAND_BY_ID_EXCEPTION.getCode(), ResultCodeEnum.BAND_BY_ID_EXCEPTION.getMsg());
////                }
////                JSONArray rows = bandInfo.getJSONArray("rows");
////                String bandName = (String) rows.getJSONObject(0).get("name");
////                draftVo.setBandName(bandName);
//
//                // 查询封装创建者信息
//                JSONObject userInfo = coreRemoteService.getUserByUserId(draftVo.getCreatorId());
//                if (userInfo == null || ((int) userInfo.get("total")) == 0) {
//                    throw new MyException(ResultCodeEnum.USER_BY_ID_EXCEPTION.getCode(), ResultCodeEnum.USER_BY_ID_EXCEPTION.getMsg());
//                }
//                JSONArray userInfoRows = userInfo.getJSONArray("rows");
//                String userName = (String) userInfoRows.getJSONObject(0).get("userName");
//                draftVo.setCreatorName(userName);
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return draftVoList;
    }

    @Override
    public Integer pageConditionDraftTotal(DraftQuery draftQuery) throws Exception {
        DraftDao draftDao = BLSAction.getBean(DraftDao.class);
        final Connection connection = BLSAction.getDBConnection();

        int count = -1;
        try {
            count = draftDao.pageConditionDraftTotal(connection, draftQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public void save(Draft draft) throws Exception {
        DraftDao draftDao = BLSAction.getBean(DraftDao.class);
        final Connection connection = BLSAction.getDBConnection();

        try {
            draftDao.save(connection, draft);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int deleteDraft(Long draftId) throws Exception {
        DraftDao draftDao = BLSAction.getBean(DraftDao.class);
        final Connection connection = BLSAction.getDBConnection();

        AnalyseService analyseService = BLSAction.getBean(AnalyseService.class);
        // 两种情况，
        int i = -1;
        Draft draft = draftDao.selectDraftById(connection, draftId);
        if (draft.getDeveloped() == 0) {
            // 1.未开发，直接删除草稿
            i = draftDao.deleteDraft(connection, draftId);
            return i;
        }

        // 2.已开发，先删除分析剧本，再删除草稿
        long anylyseId = draft.getAnalyseId();
        Analyse analyse = analyseService.selectAnalyseById(anylyseId);
        //分析剧本运行状态（0-未运行，1-运行中）
        if (analyse.getStatus() == 1) {
            throw new MyException(ResultCodeEnum.DRAFT_DELETE_EXCEPTION.getCode(), ResultCodeEnum.DRAFT_DELETE_EXCEPTION.getMsg());
        }
        analyseService.deleteById(anylyseId);
        i = draftDao.deleteDraft(connection, draftId);

        return i;
    }

    @Override
    public int updateDraft(Draft draft) throws Exception {
        DraftDao draftDao = BLSAction.getBean(DraftDao.class);
        final Connection connection = BLSAction.getDBConnection();

        int i = -1;
        try {
            i = (int) draftDao.updateIgnoreNull(connection, draft);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public Draft selectDraftById(Long draftId) throws Exception {
        DraftDao draftDao = BLSAction.getBean(DraftDao.class);
        final Connection connection = BLSAction.getDBConnection();

        return draftDao.selectDraftById(connection, draftId);
    }

    @Override
    public Analyse selectAnalyseById(Long draftId) throws Exception {
        AnalyseService analyseService = BLSAction.getBean(AnalyseService.class);
        Draft draft = this.selectDraftById(draftId);
        Analyse analyse = analyseService.selectAnalyseById(draft.getAnalyseId());
        return analyse;
    }

    @Override
    public List<DraftVo> pageConditionAnalyseRunningStatusList(DraftQuery draftQuery, Integer pageSize, Integer current)  throws Exception {

        DraftDao draftDao = BLSAction.getBean(DraftDao.class);
        final Connection connection = BLSAction.getDBConnection();
        List<DraftVo> draftVoList = null;
        try {
            draftVoList = draftDao.pageConditionAnalyseRunningStatusList(connection, draftQuery, pageSize, current);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return draftVoList;
    }

    @Override
    public Integer pageConditionAnalyseRunningStatusTotal(DraftQuery draftQuery) throws Exception {
        DraftDao draftDao = BLSAction.getBean(DraftDao.class);
        final Connection connection = BLSAction.getDBConnection();

        int count = -1;
        try {
            count = draftDao.pageConditionAnalyseRunningStatusTotal(connection, draftQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}